package newApi;

import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

class Person {
    private int age;
    private String name;

    public Person(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

public class StreamTest {
    public static void main(String[] args) {
        List<Person> persons = new ArrayList<Person>() {
            {
                add(new Person(20, "maomao"));
                add(new Person(30, "mayun"));
                add(new Person(26, "keke"));
                add(new Person(40, "angular"));
            }
        };

            // 中间流是新的流
//        Stream<Person> originStream = persons.stream();
//        Stream<Person> filterStream = originStream.filter(person -> person.getAge() > 20);
//        System.out.println(originStream); // java.util.stream.ReferencePipeline$Head@7ba4f24f
//        System.out.println(filterStream); // java.util.stream.ReferencePipeline$2@3b9a45b3
            // 转换流也是新的流
//        Stream<Person> originStream = persons.stream();
//        IntStream intStream = originStream.mapToInt(person -> person.getAge());
//        System.out.println(originStream); // java.util.stream.ReferencePipeline$Head@7ba4f24f
//        System.out.println(intStream); // java.util.stream.ReferencePipeline$4@3b9a45b3

            // 同一个中间流不可被操作多次
//        Stream<Person> originStream = persons.stream();
//        Stream<Integer> integerStream = originStream.map(person -> person.getAge());
//        Stream<Person> sortStream = originStream.sorted(); // 再次使用originStream中间流
//        System.out.println(originStream);
//        System.out.println(integerStream);
//        System.out.println(sortStream);

        // 流操作的参数无干扰、无状态
//        originStream.forEach(person -> {
//            // lambda表达式内的参数person状态更改不影响节点流
//            person.setAge(person.getAge() + 1);
//            System.out.println(person.getAge());
//        });
        // 流操作过程中参数（即forEach里lambda表达式），必须时无干扰的（不可以改变数据源）
//        originStream.forEach(person -> persons.remove(0));
//     // originStream.forEach(person -> persons.add(new Person(43, "aobama")));

        // 查询过程中修改流源
//        Stream<Person> originStream = persons.stream();
//        originStream.forEach(person -> persons.remove(person));

    }
}
